package com.easyidea.its.domain.repository.surveillance.impl;

import java.util.Collection;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.easyidea.its.domain.model.passport.TrackLog;
import com.easyidea.its.domain.model.surveillance.Alarm;
import com.easyidea.its.domain.repository.base.impl.BaseBeanRepositoryImpl;
import com.easyidea.its.domain.repository.surveillance.AlarmRepository;
import com.easyidea.its.util.StringUtil;
import com.easyidea.its.web.action.surveillance.dto.SurveillanceDto;

/**
 * 
 * @author hukn
 * 
 */
public class AlarmRepositoryImpl extends BaseBeanRepositoryImpl<Alarm> implements AlarmRepository {

	public int getAlarmCountByDto(SurveillanceDto searchDto) {
		String hql = "select count(distinct Alarm.id) " + this.getAssemblyHql(searchDto);
		return this.getBeanByHQL(Long.class, hql).intValue();
	}

	public Collection<Alarm> getAlarmByDto(SurveillanceDto searchDto, int start, int pageSize) {
		String hql = "select Alarm " + this.getAssemblyHql(searchDto);
		Collection<Alarm> surveillances = this.listBeansByHQL(Alarm.class, hql, start, pageSize);
		return surveillances;
	}

	private String getAssemblyHql(SurveillanceDto searchDto) {
		StringBuffer buffer = new StringBuffer();
		buffer.append(" from " + Alarm.class.getName() + " as Alarm ");
		
		buffer.append(" join Alarm.surveillance as Surveillance ");
		buffer.append(" , TrackLog as tl ");
		StringBuffer whereBuffer = new StringBuffer();
		whereBuffer.append(" where tl.passTime = Alarm.alarmDate and tl.passPort.passPortName = Alarm.passPortName and tl.direction = Alarm.direction");
		if (!StringUtil.isNullOrBlank(searchDto.getSurveillanceType())) {
			if (whereBuffer.length() > 0)
				whereBuffer.append(" and Surveillance.surveillanceType='" + searchDto.getSurveillanceType() + "' ");
//			else
//				whereBuffer.append("  Surveillance.surveillanceType='" + searchDto.getSurveillanceType() + "' ");
		}
		if (!StringUtil.isNullOrBlank(searchDto.getPlateNumberSufix())) {
			if (whereBuffer.length() > 0)
				whereBuffer.append(" and Surveillance.plateNumber='" + searchDto.getPlateNumberPrefix() +searchDto.getPlateNumberSufix() + "' ");
//			else
//				whereBuffer.append(" where Surveillance.plateNumber='" + searchDto.getPlateNumberPrefix() +searchDto.getPlateNumberSufix() + "' ");
		}		
		if (!StringUtil.isNullOrBlank(searchDto.getStartDate())) {
			String startDate = searchDto.getStartDate();
			startDate = startDate+" "+searchDto.getStartHour()+":"+searchDto.getStartMinute()+":00";
			if (whereBuffer.length() > 0)
				whereBuffer.append(" and Alarm.alarmDate>='" + startDate + "' ");
//			else
//				whereBuffer.append(" where Alarm.alarmDate>='" + startDate + "' ");
		}
		if (!StringUtil.isNullOrBlank(searchDto.getEndDate())) {
			String endDate = searchDto.getEndDate();
			endDate = endDate+" "+searchDto.getEndHour()+":"+searchDto.getEndMinute()+":00";
			if (whereBuffer.length() > 0)
				whereBuffer.append(" and Alarm.alarmDate<='" + endDate + "' ");
//			else
//				whereBuffer.append(" where Alarm.alarmDate<='" + endDate + "' ");
		}			
		if (!StringUtil.isNullOrBlank(searchDto.getPlateType())) {
			if (whereBuffer.length() > 0)
				whereBuffer.append(" and Surveillance.plateType='" + searchDto.getPlateType() + "' ");
//			else
//				whereBuffer.append(" where Surveillance.plateType='" + searchDto.getPlateType() + "' ");
		}
		if (!StringUtil.isNullOrBlank(searchDto.getDirection())) {
			if (whereBuffer.length() > 0)
				whereBuffer.append(" and Alarm.direction='" + searchDto.getDirection() + "' ");
//			else
//				whereBuffer.append(" where Alarm.direction>='" + searchDto.getDirection() + "' ");
		}		
		buffer.append(whereBuffer);
		return buffer.toString();
	}
	public Alarm getLatestAlarmByCondition(String passPortName, String direction) {
		Criteria ca = this.createCriteria();
	
		if (passPortName != null && !passPortName.equals("")) {
			ca.add(Restrictions.eq("passPortName", passPortName));
		}
		if (direction != null && !direction.equals("")) {
			ca.add(Restrictions.eq("direction", direction));
		}
		ca.addOrder(Order.desc("alarmDate"));
		ca.setMaxResults(1);
		return (Alarm)ca.uniqueResult();
	}
	@SuppressWarnings("unchecked")
	public List<Object[]> getLatestAlarm() {
		String hql = "select tl.plateNumber,tl.plateType,tl.passTime," 
		+" tl.passPort.passPortName,tl.laneName,tl.speed,tl.direction,al.id from " 
			+TrackLog.class.getName() + " as tl,"
			+ Alarm.class.getName() + " as al "
			+ " where al.dealWith = false and" 
			+ " tl.passTime = al.alarmDate and "
			+ " tl.passPort.passPortName = al.passPortName and"
			+ " tl.direction = al.direction order by  al.alarmDate desc";
		Query q = this.getCurrentSession().createQuery(hql);
		q.setFirstResult(0);
		q.setMaxResults(10);
		return q.list();
	}
	public boolean doDealWithAlarm(Integer alarmId) {
		Alarm alarm = this.getBeanById(alarmId);
		alarm.setDealWith(true);
		this.getCurrentSession().update(alarm);
		return true;
	}
}